#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include "debug.h"
#include "os_api.h"
#include "ps_api.h"
#include "dev_api.h"

#include "atclient.h"


#define _STACK_SIZE	(1024*3)
static void* _task_stack = NULL;
static OSTaskRef _task_ref = NULL;

typedef struct{    
    int len;
    UINT8 *data;
}commonIndParam;

#define COM_STACK_SIZE	(1024*3)
static void* com_task_stack = NULL;
static OSTaskRef com_task_ref = NULL;
static OSMsgQRef commonIndMsgQ = NULL;


void wait_network_ready(void)
{
    int count = 30;
    while(count-- && ps_GetNetworkReady() == FALSE){	
        OSATaskSleep(200);
    }

    if (count <= 0){
        LOG_PRINTF("dev_CtrlReset.\n");
        dev_CtrlReset(0);
    }
}

static void _task(void *ptr)
{	
	int ret;
	char resp_str[128]={'\0'};
	
	int need_wait_ims_ready = 0;
	int ims_ready = 0;
	
	wait_network_ready();	
	
	// +COPS?	
	ret = atclient_send("AT+COPS?\r\n", 3, 1, resp_str, sizeof(resp_str));
	LOG_PRINTF("%s: resp_str = %s, ret = %u\n",__FUNCTION__,resp_str,ret);
	if(strstr(resp_str, "46011") != NULL ){
		need_wait_ims_ready = 1;
	}
	
	// +CIREG?
	while (need_wait_ims_ready && !ims_ready){
		ret = atclient_send("AT+CIREG?\r\n", 3, 1, resp_str, sizeof(resp_str));
		LOG_PRINTF("%s: resp_str = %s, ret = %u\n",__FUNCTION__,resp_str,ret);
		if(strstr(resp_str, "+CIREG: 1,1,") != NULL ){
			ims_ready = 1;
		}
		OSATaskSleep(200);
	}
	
	// +CNMI
	ret = atclient_send("AT+CNMI=1,2,0,0,0\r\n", 3, 1, resp_str, sizeof(resp_str));
	LOG_PRINTF("%s: resp_str = %s, ret = %u\n",__FUNCTION__,resp_str,ret);
	
	// +CMGF?
	ret = atclient_send("AT+CMGF=1\r\n", 3, 1, resp_str, sizeof(resp_str));
	LOG_PRINTF("%s: resp_str = %s, ret = %u\n",__FUNCTION__,resp_str,ret);
	
	ret = atclient_send("AT+CMGF?\r\n", 3, 1, resp_str, sizeof(resp_str));
	LOG_PRINTF("%s: resp_str = %s, ret = %u\n",__FUNCTION__,resp_str,ret);
	
	// +CMGS	// 注意okFlag设置成0
	ret = atclient_send("AT+CMGS=\"1312276xxxx\"\r\n", 3, 0, resp_str, sizeof(resp_str));
	LOG_PRINTF("%s: resp_str = %s, ret = %u\n",__FUNCTION__,resp_str,ret);	

	if(strstr(resp_str, ">") != NULL && (ret == 0)){
		char sms_str[128]={'\0'};

		sms_str[0] = 0x31;
		sms_str[1] = 0x31;
		sms_str[2] = 0x31;
		sms_str[3] = 0x31;
		sms_str[4] = 0x31;
		sms_str[5] = 0x1A;  // 0x1A 是短信内容输入的结束符，之前是短信内容
		
		ret = atclient_send(sms_str, 30, 1, resp_str, sizeof(resp_str));
		LOG_PRINTF("%s: #5# resp_str = %s, ret = %u\n",__FUNCTION__,resp_str,ret);
	}	
}

void atIndCallback(const char *indication)
{	
	OSA_STATUS osa_status;
	commonIndParam commonInd_data = {0};

    commonInd_data.data = strdup(indication);
    commonInd_data.len = strlen(indication);
	
    osa_status = OSAMsgQSend(commonIndMsgQ, sizeof(commonIndParam), (UINT8*)&commonInd_data, OSA_NO_SUSPEND);
    ASSERT(osa_status == OS_SUCCESS);
}

static void com_task(void *ptr)
{
	int ret;
    int rcv = 0;    
    OSA_STATUS status;
	commonIndParam commonInd_temp;	

	atclient_set_ind_cb(atIndCallback);
	
    while (1) {
        memset(&commonInd_temp, 0, sizeof(commonIndParam));        
        status = OSAMsgQRecv(commonIndMsgQ, (UINT8 *)&commonInd_temp, sizeof(commonIndParam), OSA_SUSPEND);
        
        if (status == OS_SUCCESS) {
            if (commonInd_temp.data) {
                LOG_PRINTF("%s[%d]: commonInd_temp len:%d, data:%s\n", __FUNCTION__, __LINE__, commonInd_temp.len, (char *)(commonInd_temp.data));
								
				//sms URC: +CMTI、+CMT、+CDS
				
				if (strstr((char *)commonInd_temp.data, "+CMTI")
					||strstr((char *)commonInd_temp.data, "+CMT")
					||strstr((char *)commonInd_temp.data, "+CDS")){
						
					LOG_PRINTF("SMS URC: len:%d, data:%s\n", commonInd_temp.len, (char *)(commonInd_temp.data));	
				}
				
				free(commonInd_temp.data);
            }
        }
    }
}


int main(void)
{
    int ret;
	
	_task_stack = malloc(_STACK_SIZE);
	ASSERT(_task_stack != NULL);
	
    ret = OSATaskCreate(&_task_ref, _task_stack, _STACK_SIZE, 100, "_task", _task, NULL);
    ASSERT(ret == OS_SUCCESS);
			
    ret = OSAMsgQCreate(&commonIndMsgQ, "commonIndMsgQ", sizeof(commonIndParam), 300, OS_FIFO);
    ASSERT(ret == OS_SUCCESS);	
	
	com_task_stack = malloc(COM_STACK_SIZE);
	ASSERT(com_task_stack != NULL);
	
    ret = OSATaskCreate(&com_task_ref, com_task_stack, COM_STACK_SIZE, 100, "com_task", com_task, NULL);
    ASSERT(ret == OS_SUCCESS);	

	return 0;
}
